package com.sgai.zero_robot.pop;

import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.sgai.zero_robot.R;
import  com.sgai.zero_robot.utils.DeviceUtils;


/**
 * Describe :
 * Created by zzw on 2024/3/21
 */
public class LoadingDialog extends BaseDialog implements DialogInterface.OnDismissListener {
    private AppCompatTextView tvContent;
    private AppCompatImageView mLoading;
    private RotateAnimation rotateAnimation;

    public LoadingDialog(Context context) {
        super(context);
    }

    @Override
    public int getDialogHeight() {
        return DeviceUtils.dp2px(getContext(), 50);
    }

    public void setContent(String msg) {
        if (tvContent != null) {
            tvContent.setText(msg);
        }
    }

    @Override
    public int getDialogWidth() {
        return DeviceUtils.dp2px(getContext(), 189);
    }

    @Override
    protected void initContentView(View mContentView) {
        if (mContentView != null) {
            tvContent = mContentView.findViewById(R.id.tv_content);
        }
        setOnDismissListener(this);
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(R.drawable.shape_26000000_5);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.loading_layout;
    }

    @Override
    public boolean getBackgroundDimEnabled() {
        return false;
    }

    @Override
    public void show() {
        super.show();
        rotateAnimation = new RotateAnimation(
                0f,
                360f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
        );
        // 设置动画持续时间
        if (rotateAnimation != null) {
            rotateAnimation.setDuration(1000);
            // 设置动画重复次数
            rotateAnimation.setRepeatCount(-1);
            // 应用动画到视图
            if (mLoading != null) {
                mLoading.startAnimation(rotateAnimation);
            }
        }
    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mLoading != null) {
            mLoading.clearAnimation();
        }
    }
}